﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SortArrayDouble
{
    class BubbleSortDouble
    {
        public static void Sort(double[] source)
        {
            double d;
            for (int i = 0; i < source.Length; i++)
            {
                for (int j = source.Length - 1; j > i; j--)
                {
                    if (source[j - 1] > source[j])
                    {
                        d = source[j - 1];
                        source[j - 1] = source[j];
                        source[j] = d;
                    }
                }
            }
        }
    }

    class QuickSortDouble
    {
        public static void Sort(double[] source, long FirstPos, long LastPos)
        {
            double d;
            double MiddleElement = source[(LastPos - FirstPos) / 2 + FirstPos];
            long i = FirstPos, j = LastPos;

            while (i <= j)
            {
                while (source[i] < MiddleElement && i <= LastPos)
                   ++i;
                while (source[j] > MiddleElement && j >= FirstPos)
                   --j;
                if (i <= j)
                {
                    d = source[i];
                    source[i] = source[j];
                    source[j] = d;
                    ++i;
                    --j;
                }
            }
            if (j > FirstPos)
                Sort(source, FirstPos, j);
            if (i < LastPos)
                Sort(source, i, LastPos);
        }
    }

    class Program
    {       
        static void Main(string[] args)
        {
            int MaxLen = 10;
            double[] InputListToSort;
            double[] OutputListToSort;

            Console.Write("Input Number of elements: ");
            MaxLen = int.Parse(Console.ReadLine());

            InputListToSort = new double[MaxLen];
            OutputListToSort = new double[MaxLen];

            Console.WriteLine("\nSource Array of double:");
            // Generate Randomize Array of Double
            Random fixRand = new Random();
            for (int i = 0; i < InputListToSort.Length; i++)
            {
                InputListToSort[i] = fixRand.NextDouble() * 100;
                OutputListToSort[i] = InputListToSort[i];
                Console.WriteLine(InputListToSort[i]);
            }
            BubbleSortDouble.Sort(OutputListToSort);

            // Print BubbleSort result
            Console.WriteLine("\nBubble Sort Array:");
            foreach(double d in OutputListToSort)
            {
                Console.WriteLine(d);
            }

            // Copy InputListToSort to OutputListToSort before Quick Sorting
            InputListToSort.CopyTo(OutputListToSort, 0);

            QuickSortDouble.Sort(OutputListToSort, 0, OutputListToSort.Length-1);
            // Print QuickSort result
            Console.WriteLine("\nQuick Sort Array:");
            foreach (double d in OutputListToSort)
            {
                Console.WriteLine(d);
            }
            Console.ReadKey(true);
        }
    }
}
